package com.sx.micowell.framework.model

import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.service.IApiService
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.*
import com.sx.micowell.framework.adapter.binding.bean.FragmentTitleArgument
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction
import com.sx.micowell.module.course.ChildCourseListFragment
import kotlin.streams.toList

/**
 * @Description 推荐课程
 * @Author 梁浩文
 * @Date 2021/8/24 10:34
 */
class RecommendCourseModel : BaseViewModel() {
    var currentMp4= String()
    var currentURL= String()
    // 页面
    var pageListData = MutableLiveListData<FragmentTitleArgument>()

    // 课程类型
    var courseTypeList = MutableLiveListData<DictType>()

    // 课程列表
    var courseList = MutableLiveListData<CourseInfo>()

    // 视频数据
    var courseVideoData: SingleLiveEvent<CourseVideoVO> = SingleLiveEvent()

    // 收藏事件
    var collectionEvent: SingleLiveEvent<Boolean> = SingleLiveEvent();

    // 头像点击事件
    val moreCourseOnClickCommand = BindingCommand<Any>(IBindingAction {
        context.startContainerActivity(
            (RouteManager.getInstance().getMoreCourseFragment().javaClass.canonicalName)
        )
    })

    //初始化页面
    fun initPage() {
        val pageData = MutableLiveListData.Builder(pageListData)
        courseTypeList.value?.forEach {
            pageData.add(
                FragmentTitleArgument(
                    RouteManager.getInstance().getChildCourseListFragment(it.dictValue)
                )
            )
        }
        pageData.buildPost()
    }

    fun initMorePage() {
        val pageData = MutableLiveListData.Builder(pageListData)
        courseTypeList.value?.forEach {
            pageData.add(
                FragmentTitleArgument(
                    RouteManager.getInstance().getMoreChildCourseListFragment(it.dictValue)
                )
            )
        }
        pageData.buildPost()
    }

    /*请求课程类型*/
    fun reqCourseType() {
        HttpClient.instance.create(IApiService::class.java)
            .getCourseTypeList()
            .compose(RxUtils.compose())
            .subscribe(object : ApiResponse<CourseTypeListBean>() {
                override fun success(data: CourseTypeListBean?, msg: String?) {
                    data.let { courseTypeList.postValue(it?.dictType) }
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                }
            })
    }

    /*请求课程类型*/
    fun reqCourseList(curriculumType: String) {
        HttpClient.instance.create(IApiService::class.java)
            .getCurriculumList(curriculumType)
            .compose(RxUtils.compose())
            .subscribe(object : ApiResponse<CourseListBean>() {
                override fun success(data: CourseListBean?, msg: String?) {
                    courseList.postValue(data?.info?.stream()?.map { mapToCourseInfo(it) }
                        ?.toList())
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                }

            })
    }

    fun mapToCourseInfo(courseInfo: CourseInfo): CourseInfo {
        courseInfo.curriculumCover = courseInfo.curriculumCover.getUrl()
        courseInfo.description =
            "${courseInfo.duration}分钟·${courseInfo.calorie}千卡·${courseInfo.basicConditions}零基础"
        return courseInfo
    }

    /**
     * 获取视频课程
     */
    fun getCourseVideo(id: String) {
        addSubscribe(
            HttpClient.instance.apiService
                .getCourseVideo(id)
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<CourseVideoVO>() {
                    override fun success(data: CourseVideoVO?, msg: String?) {
                        courseVideoData.postValue(data)
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {

                    }

                })
        )
    }

    /**
     * 添加收藏
     */
    fun addCollection(id: String) {
        addSubscribe(
            HttpClient.instance.apiService
                .addCollection(id)
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<Void?>() {
                    override fun success(data: Void?, msg: String?) {
                        ToastUtils.showShort("添加成功")
                        collectionEvent.value = true
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {

                    }

                })
        )
    }

    /**
     * 取消收藏
     */
    fun cancelCollection(id: String) {
        addSubscribe(HttpClient.instance.apiService
            .cancelCollection(id)
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<Void?>() {
                override fun success(data: Void?, msg: String?) {
                    ToastUtils.showShort("取消收藏")
                    collectionEvent.value = false
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {

                }

            })
        )
    }

}